/*
 * @Author: mengxiangji
 * @Email: 397082664@qq.com
 * @Date: 2020-06-17 15:05:27
 * @Last Modified by: mxj
 * @Last Modified time: 2020-07-28 09:35:21
 * @Description: 教材章节页
 */

//使用方法：
// List<int> selectids=[76,77];//当前选中章节ID数组
// showBottomSheetDialog(new SelectBookMenu(bookId: "5",bookName: "人教版初中七年级地理上册",selectids:selectids,callback: selectmenucallback));
// GetSelectedBookMenu selectmenucallback(List<BookMenu> selectlist){print("回调"+selectlist.length.toString());} //选中后回调函数。

import 'package:flutter/material.dart';
import 'package:flutter_code_framework/model_data/bookmenu/bookmenu.dart';
import 'package:flutter_code_framework/utils/global.dart';

typedef void GetSelectedBookMenu(List<BookMenu> selectlist);

class SelectBookMenu extends StatefulWidget {
  String bookId;
  String bookName;
  List<int> selectids;
  GetSelectedBookMenu callback;
  SelectBookMenu(
      {Key key,
      this.bookId,
      this.bookName,
      this.selectids,
      @required this.callback})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SelectBookMenuState();
  }
}

class _SelectBookMenuState extends State<SelectBookMenu> {
  List _menulist = [];
  @override
  Widget build(BuildContext context) {
    Widget headtitle = Container(
      height: 56,
      padding: EdgeInsets.fromLTRB(24, 0, 24, 0),
      width: double.infinity,
      child: Row(
        children: <Widget>[
          Expanded(
              flex: 1,
              child: Container(
                  child: Text(widget.bookName,
                      overflow: TextOverflow.ellipsis,
                      softWrap: false,
                      style: TextStyle(
                          color:
                              Theme.of(context).primaryTextTheme.caption.color,
                          fontSize: 18.0)))),
          GestureDetector(
              child: Container(
                  margin: EdgeInsets.only(left: 5.0, right: 10),
                  color: Colors.transparent,
                  child: Text(
                    '清空',
                    style: TextStyle(
                        color:
                            Theme.of(context).primaryTextTheme.headline5.color,
                        fontSize: 18.0),
                  )),
              onTap: () {
                setState(() {
                  this._menulist.forEach((element) {
                    element.ischecked = false;
                  });
                });
                widget.callback(List<BookMenu>());
              }),
          GestureDetector(
              child: Container(
                  color: Colors.transparent,
                  child: Text(
                    '全选',
                    style: TextStyle(
                        color:
                            Theme.of(context).primaryTextTheme.headline5.color,
                        fontSize: 18.0),
                  )),
              onTap: () {
                List<BookMenu> tmpselectmenu = new List<BookMenu>();
                setState(() {
                  this._menulist.forEach((element) {
                    element.ischecked = true;
                    tmpselectmenu.add(new BookMenu(
                        id: element.id,
                        parentid: element.parentid,
                        level: element.level,
                        name: element.name,
                        ischecked: element.ischecked));
                  });
                });
                widget.callback(tmpselectmenu);
              })
        ],
      ),
    );

    Widget linecontainer = Container(
        height: 1,
        width: double.infinity,
        color: Theme.of(context).dividerColor,
        child: Text(''));

    Widget menucontainer = Container(
        height: MediaQuery.of(context).size.height * 0.8 - 60,
        padding: EdgeInsets.fromLTRB(8, 10, 8, 24),
        width: double.infinity,
        child: ListView(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          children: List.generate(_menulist.length, (index) {
            return Container(
              height: this._menulist[index].level == 1 ? 46 : 41,
              child: CheckboxListTile(
                value: this._menulist[index].ischecked,
                onChanged: (isCheck) => {
                  setState(() {
                    this._menulist[index].ischecked = isCheck;
                    List<BookMenu> tmpselectmenu = new List<BookMenu>();
                    this
                        ._menulist
                        .where((element) => element.ischecked == true)
                        .toList()
                        .forEach((element) {
                      tmpselectmenu.add(new BookMenu(
                          id: element.id,
                          parentid: element.parentid,
                          level: element.level,
                          name: element.name,
                          ischecked: element.ischecked));
                    });
                    widget.callback(tmpselectmenu);
                  })
                },
                activeColor: Theme.of(context).primaryTextTheme.headline5.color,
                title: Text(this._menulist[index].name,
                    overflow: TextOverflow.ellipsis,
                    softWrap: false,
                    style: TextStyle(
                        color: (this._menulist[index].level == 1
                            ? Theme.of(context).primaryTextTheme.caption.color
                            : Theme.of(context)
                                .primaryTextTheme
                                .caption
                                .color
                                .withAlpha(230)),
                        fontSize:
                            this._menulist[index].level == 1 ? 18.0 : 15.0)),
                //是否要撑满3行
                isThreeLine: false,
                //是否密集  true 居中 ； fase 非居中
                dense: true,
                selected: false,
                //选择控件放的位置
                controlAffinity: ListTileControlAffinity.platform,
              ),
            );
          }),
        ));

    return Container(
      height: MediaQuery.of(context).size.height * 0.8,
      color: Colors.transparent,
      child: ListView(
        children: <Widget>[headtitle, linecontainer, menucontainer],
      ),
    );
  }

  //获取章节数据
  _getBookMenuNode() async {
    if (widget.bookId != "" &&
        widget.bookId != null &&
        widget.bookId != "null") {
      var _bookMenu =
          await G.req.bookMenuData.getBookMenuList(bookId: widget.bookId);
      setState(() {
        _menulist.clear();
        if (_bookMenu.data['menulist'].length > 0) {
          _menulist.addAll(_bookMenu.data["menulist"]
              .map<BookMenu>((e) => BookMenu.fromJson(e))
              .toList());

          if (widget.selectids != null) {
            widget.selectids.forEach((menuid) {
              var tmpmenu =
                  _menulist.firstWhere((element) => element.id == menuid);
              if (tmpmenu != null) {
                tmpmenu.ischecked = true;
              }
            });
          }
        }
      });
    }
  }

  @override
  void initState() {
    super.initState();
    _getBookMenuNode();
  }

  @override
  void dispose() {
    super.dispose();
  }
}
